@alwith/module-chat 0.1.6 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,4 +3,4 @@ import{jsx as i,jsxs as h,Fragment as oe}from"react/jsx-runtime";import{decodeHT
3
3
  \`\`\``}function j(e,n){if(!e.includes(n)||Ee(e,n)%2===0)return e;let t=Te(e,n);if(t<0)return e;let r=e.slice(t+n.length);if(r.length===0||/^\s/.test(r)||r.includes(`
4
4
  `)||J(r))return e;return`${e}${n}`}function ve(e){return e.startsWith("/")&&!e.startsWith("//")||/^[A-Za-z]:[\\/]/.test(e)||/^\\\\[^\\]+\\[^\\]+/.test(e)||/^\/\/[^/]+\/[^/]+/.test(e)}function Y(e){let n=e.replace(/`/g,"").trim(),t=n.match(/^(.*?):(\d+)(?::(\d+))?(?:[-–](\d+)(?::(\d+))?)?$/);if(t!==null){let[,o,a,s,c,d]=t;if(o===void 0||a===void 0)throw Error("Codex file location is incomplete");return{path:o,line:Number.parseInt(a,10),column:s===void 0?void 0:Number.parseInt(s,10),endLine:c===void 0?void 0:Number.parseInt(c,10),endColumn:d===void 0?void 0:Number.parseInt(d,10)}}let r=n.match(/^(.*?)#L(\d+)(?:C(\d+))?(?:-L(\d+)(?:C(\d+))?)?$/);if(r!==null){let[,o,a,s,c,d]=r;if(o===void 0||a===void 0)throw Error("Codex hash file location is incomplete");return{path:o,line:Number.parseInt(a,10),column:s===void 0?void 0:Number.parseInt(s,10),endLine:c===void 0?void 0:Number.parseInt(c,10),endColumn:d===void 0?void 0:Number.parseInt(d,10)}}return{path:n}}function Le(e){let{path:n,line:t,column:r,endLine:o,endColumn:a}=Y(e);if(/^[a-z][a-z0-9+.-]*:\/\//i.test(n)||/^www\./i.test(n)||/^(mailto|tel):/i.test(n))return!1;if(t!==void 0||r!==void 0||o!==void 0||a!==void 0)return!0;if(!/(?:^|[\\/])[^\\/]+\.(?!\d+$)[^\\/.]+$/i.test(n))return!1;let s=/[\\/]/.test(n),c=n.split(/[\\/]/).at(-1);if(c===void 0)throw Error("Codex streaming link target has no file name");let d=c.split(".").at(-1);if(d===void 0||d.length===0)return!1;if(!s&&/[a-z]/.test(d)&&/[A-Z]/.test(d.slice(1)))return!1;if(!s){let u=d.toLowerCase();if(u.length>Ne&&!Me.has(u))return!1}return!0}function Se(e){let{path:n}=Y(e);if(ve(n))return!0;try{return new URL(n).protocol==="file:"}catch{return!1}}function _e(e){let n=e.match(F);if(n===null)return e;let t=n[2];if(t===void 0)throw Error("Codex streaming link target is missing");return Le(t)||Se(t)?e.replace(F,"$1"):`${e})`}function Ae(e){let n=e.replace(Ce,"");if(n.length===0||n.includes("`")&&J(n))return{isCodeFenceOpen:!1,markdown:n};if(n.includes("```")&&ye(n))return{isCodeFenceOpen:!0,markdown:Ie(n)};if(n.includes("!["))n=n.replace(be,(t,r)=>r);if(n.includes("]("))n=_e(n);return n=j(n,"**"),n=j(n,"*"),{isCodeFenceOpen:!1,markdown:n}}function Fe(e){return e?.type==="paragraph"&&"tokens"in e&&Array.isArray(e.tokens)}function O(e){return Fe(e)&&e.tokens.length===1&&e.tokens[0]?.type==="image"}function Q(e){for(let n=0;n<e.length;n+=1){let t=e[n];if(t===void 0)throw Error(`Codex Markdown token is missing at index ${n}`);if("tokens"in t&&Array.isArray(t.tokens))Q(t.tokens);if(!O(t))continue;let r=[...t.tokens],o=n+1,a=o;for(;;){while(e[o]?.type==="space")o+=1;let s=e[o];if(!O(s))break;r.push(...s.tokens),o+=1,a=o}if(a===n+1)continue;t.tokens=r,t.text=r.map((s)=>s.raw).join(`
5
5
  `),t.raw=e.slice(n,a).map((s)=>s.raw).join(""),e.splice(n+1,a-n-1)}}function R(e){return{type:"text",raw:e,text:e}}function je(e){return{type:"paragraph",raw:e.raw,text:e.text,tokens:e.tokens??[e]}}function I(e){for(let n of e){if(n.type==="list")for(let t of n.items){let r=t.tokens[0];if(r?.type==="heading"){let o=r.raw.trimEnd(),a=o.match(/^#{1,6}\s+/)?.[0];if(a!==void 0)t.tokens[0]={type:"paragraph",raw:r.raw,text:o,tokens:r.tokens===void 0?[R(o)]:[R(a),...r.tokens]}}if(t.loose)t.tokens=t.tokens.map((o)=>o.type==="text"?je(o):o);I(t.tokens)}if("tokens"in n&&Array.isArray(n.tokens))I(n.tokens)}}function Oe(e,n,t){e.preventDefault(),e.stopPropagation(),t.openLink(n)}function Re(e){try{let n=new URL(e);return n.protocol!=="http:"&&n.protocol!=="https:"?null:`${n.origin}/favicon.ico`}catch{return null}}function Pe(e){let n=Re(e);return n===null?[]:[n,`https://www.google.com/s2/favicons?domain=${new URL(e).origin}&sz=32`]}function qe({href:e}){let n=B(()=>Pe(e),[e]),[t,r]=p(),o=n.length===0&&!/^[a-z][a-z0-9+.-]*:/i.test(e),a=e.toLowerCase().startsWith("mailto:")?me:o?le:ue;return h("span",{className:"codex-markdown-favicon",children:[t===void 0?i("span",{className:"codex-markdown-favicon-fallback",children:i(a,{"aria-hidden":!0})}):null,n.map((s)=>i("img",{alt:"",className:t===s?"codex-markdown-favicon-image-loaded":void 0,decoding:"async",draggable:!1,referrerPolicy:"no-referrer",src:s,onLoad:()=>r((c)=>c!==void 0&&n.includes(c)?c:s)},s))]})}function We({children:e,href:n,title:t}){let r=V();return i("a",{className:"codex-markdown-link","data-inline-mention-interactive":"",href:n,title:t,onClick:(o)=>Oe(o,n,r),children:h("span",{className:"codex-markdown-link-mention","data-layout":"inline-flow","data-interactive":"","data-underline-on-hover":"",children:[i("span",{className:"codex-markdown-link-icon-container","data-markdown-copy":"exclude",children:i(qe,{href:n})}),i("span",{className:"codex-markdown-link-label",children:e})]})})}function ze({code:e,language:n}){let{theme:t,copyLabel:r}=V(),o=w(null),[a,s]=p(null),[c,d]=p(null),[u,k]=p(!1);if(g(()=>{let f=we(s,d);return o.current=f,()=>{f.dispose(),o.current=null}},[]),g(()=>{let f=o.current;if(f===null)throw Error("Code highlighter is not mounted");f.update(e,n)},[e,n]),c!==null)throw c;let b=a!==null&&a.code===e&&a.language===n?a.html:null,C=()=>{navigator.clipboard.writeText(e),k(!0),window.setTimeout(()=>k(!1),2000)};return h("div",{className:"codex-markdown-code-block","data-markdown-copy":"code-block","data-markdown-copy-text":e,"data-theme":t==="dark"?"dark":"light",children:[h("div",{className:"codex-markdown-code-header","data-markdown-copy":"exclude",children:[i("div",{className:"codex-markdown-code-title",children:n}),i("div",{className:"codex-markdown-code-actions",children:i("button",{type:"button",className:"codex-markdown-code-copy",title:r,onClick:C,children:u?i(K,{}):i(ce,{})})})]}),i("div",{className:"codex-markdown-code-container",dir:"ltr",children:i("code",{className:"codex-markdown-code-content hljs",...b===null?{children:e}:{dangerouslySetInnerHTML:{__html:b}}})})]})}function Ke({code:e,language:n}){let t=w(null),[r,o]=p(!1);if(g(()=>{if(r)return;let a=t.current;if(a===null)throw Error("Codex code block placeholder is not mounted");if(typeof IntersectionObserver>"u"){let c=window.setTimeout(()=>o(!0),0);return()=>window.clearTimeout(c)}let s=new IntersectionObserver((c)=>{if(!c.some((d)=>d.isIntersecting))return;o(!0),s.disconnect()},{rootMargin:"600px 0px"});return s.observe(a),()=>s.disconnect()},[r]),r)return i(ze,{code:e,language:n});return i("div",{ref:t,className:"codex-markdown-code-block",children:i("pre",{className:"codex-markdown-code-placeholder",children:i("code",{children:e})})})}function Ue(e){let n=Array.from(document.body.querySelectorAll('[data-markdown-image-preview-trigger="true"]')),t=n.map((o)=>{let a=o.querySelector("img");if(a===null)throw Error("Codex image preview trigger has no image");return{src:a.currentSrc||a.src,alt:a.alt}}),r=n.indexOf(e);if(r===-1)throw Error("Codex image preview trigger is outside the document");return{images:t,index:r}}function He({preview:e,setPreview:n}){let t=e.images[e.index];if(t===void 0)throw Error(`Codex image preview index is out of range: ${e.index}`);return h("div",{className:"codex-markdown-image-preview",children:[i("img",{src:t.src,alt:t.alt}),h("div",{className:"codex-markdown-image-preview-actions",children:[i("button",{type:"button","aria-label":"Previous image",disabled:e.index===0,onClick:()=>n({...e,index:e.index-1}),children:i(ie,{})}),i("a",{href:t.src,download:!0,"aria-label":"Download image",children:i(de,{})}),i("button",{type:"button","aria-label":"Next image",disabled:e.index===e.images.length-1,onClick:()=>n({...e,index:e.index+1}),children:i(se,{})}),i("button",{type:"button","aria-label":"Close image preview",onClick:()=>n(void 0),children:i(he,{})})]})]})}function Xe({src:e,alt:n,title:t,allowWide:r}){let o=w(null),[a,s]=p();g(()=>{let d=o.current;if(d===null)throw Error("Codex image preview dialog is not mounted");if(a===void 0){if(d.open)d.close();return}if(!d.open)d.showModal()},[a]);let c=n.length===0?"Open image preview":n;return h(oe,{children:[i("button",{type:"button",className:`codex-markdown-image-trigger${r?"codex-markdown-image-trigger-wide":""}`,"aria-label":c,"data-markdown-image-preview-trigger":"true",onClick:(d)=>s(Ue(d.currentTarget)),children:i("img",{className:`codex-markdown-image${r?"codex-markdown-image-wide":""}`,src:e,alt:n,title:t,loading:"lazy"})}),i("dialog",{ref:o,className:"codex-markdown-image-dialog","aria-label":c,onClose:()=>s(void 0),children:a===void 0?null:i(He,{preview:a,setPreview:s})})]})}function Be({value:e,display:n}){let t=w(null);return X(()=>{if(t.current===null)throw Error("Codex math root is not mounted");ae.render(e,t.current,{displayMode:n,throwOnError:!1})},[n,e]),i("span",{ref:t,className:n?"codex-markdown-math-block":"codex-markdown-inline-math"})}var De=150,N=null;function P(e,n){let t=N;if(t===null)return e;let r=t.cursor,o=r+e.length;t.cursor=o;let a=[],s=r;for(let c of t.runs){if(c.end<=r)continue;if(c.start>=o)break;let d=Math.max(r,c.start),u=Math.min(o,c.end);if(s<d)a.push(e.slice(s-r,d-r));a.push(i("span",{className:"codex-markdown-fade",children:e.slice(d-r,u-r)},`${n}:${t.generation}:${c.start}`)),s=u}if(s<o)a.push(e.slice(s-r));return a}function ee(e){let n=5381;for(let t=0;t<e.length;t+=1)n=(n*33+e.charCodeAt(t))%4294967296;return n.toString(36)}function q(e){let n=e.trim();if(n.length===0)return"";let t=n.split(`
6
- `,1)[0];if(t===void 0)throw Error("Codex Markdown token has no first line");let r=t.match(/\S+/)?.[0];return(r===void 0?t:r).slice(0,xe)}function Ge(e){switch(e.type){case"code":return e.raw.trimStart().match(/^(```|~~~)/)?.[1]??"";case"link":return e.href;case"image":{let n=e;return`${n.href}:${q(n.text)}`}case"html":case"space":case"br":case"hr":case"def":return e.raw;case"math":return q(e.text);default:return""}}function ne(e){return`${e.type==="math"?`math-${e.display?"block":"inline"}`:e.type}-${ee(Ge(e))}`}function m(e,n){let t=new Map;return e.map((r)=>{let o=ne(r),a=t.get(o)??0;return t.set(o,a+1),te(r,`${n}-${o}-${a}`)})}function Ve(e,n){let t=e.task?e.tokens.filter((r)=>r.type!=="checkbox"):e.tokens;return h("li",{className:`codex-markdown-list-item${e.task?"codex-markdown-task-list-item":""}`,children:[e.task?h("div",{className:"codex-markdown-task-checkbox",children:[i("input",{type:"checkbox",className:"codex-markdown-checkbox",checked:e.checked,"data-state":e.checked?"checked":"unchecked",readOnly:!0,disabled:!0}),e.checked?i(K,{}):null]}):null,m(t,n)]},n)}function Ze(e,n){let r=`codex-markdown-list ${e.items.some((a)=>a.task)?"codex-markdown-task-list":e.ordered?"codex-markdown-ordered-list":"codex-markdown-unordered-list"}`,o=m(e.items,n);return e.ordered?i("ol",{className:r,start:e.start===""?void 0:e.start,children:o},n):i("ul",{className:r,children:o},n)}function W(e,n,t){let r=t?"th":"td",o=/^\d+$/.test(e.text);return U(r,{key:n,className:`${t?"codex-markdown-table-header":"codex-markdown-table-cell"}${o?" codex-markdown-numeric-table-cell":""}`,align:e.align??void 0},...m(e.tokens,n))}function Je(e,n){let t=new Map;return i("div",{className:"codex-markdown-table-container","data-markdown-table":!0,tabIndex:-1,children:i("div",{className:"codex-markdown-table-scroller",children:i("div",{className:"codex-markdown-table-wrapper",children:h("table",{className:"codex-markdown-table",children:[i("thead",{children:i("tr",{className:"codex-markdown-table-row",children:e.header.map((r,o)=>W(r,`${n}-header-${o}`,!0))})}),i("tbody",{className:"codex-markdown-table-body",children:e.rows.map((r)=>{let o=ee(r.map((c)=>c.text).join("\x00")),a=t.get(o)??0;t.set(o,a+1);let s=`${n}-row-${o}-${a}`;return i("tr",{className:"codex-markdown-table-row",children:r.map((c,d)=>W(c,`${s}-cell-${d}`,!1))},s)})})]})})})},n)}function z(e,n,t){let r=M(e.href);if(!D(r))return null;return i(Xe,{src:r,alt:v(e.text),title:e.title==null?void 0:M(e.title),allowWide:t},n)}function te(e,n){switch(e.type){case"space":case"def":case"internalMessage":return null;case"hr":return i("hr",{className:"codex-markdown-horizontal-rule"},n);case"heading":{let t=e,o={1:"h1",2:"h2",3:"h3",4:"h4",5:"h5",6:"h6"}[t.depth];if(o===void 0)throw Error(`Unsupported Codex Markdown heading depth: ${t.depth}`);return U(o,{key:n,className:`codex-markdown-heading codex-markdown-heading-${t.depth}`},...m(t.tokens,n))}case"paragraph":{let t=e,r=t.tokens.filter((s)=>s.type!=="space"&&s.type!=="br"),o=r.length>0&&r.every((s)=>s.type==="image"),a=o&&r.length>1;return i("p",{className:`codex-markdown-paragraph${o?"codex-markdown-media-paragraph codex-markdown-media-wide-block":""}${a?"codex-markdown-media-grid-paragraph":""}`,"data-markdown-image-grid":a?"true":void 0,"data-wide-markdown-block":o?"true":void 0,"data-wide-markdown-block-kind":o?"image":void 0,children:o?r.map((s,c)=>z(s,`${n}-image-${c}`,!0)):m(t.tokens,n)},n)}case"text":{let t=e;return t.tokens===void 0?P(v(t.text),n):m(t.tokens,n)}case"escape":return P(e.text,n);case"strong":return i("strong",{className:"codex-markdown-strong",children:m(e.tokens,n)},n);case"em":return i("em",{children:m(e.tokens,n)},n);case"del":return i("del",{children:m(e.tokens,n)},n);case"codespan":return i("span",{className:"codex-markdown-inline-code",dir:"ltr","data-markdown-copy":"inline-code",children:e.text},n);case"br":return i("br",{},n);case"link":{let t=e,r=m(t.tokens,n),o=M(t.href);return D(o)?i(We,{href:o,title:t.title==null?void 0:M(t.title),children:r},n):i("span",{children:r},n)}case"image":return z(e,n,!1);case"code":{let t=e;return i(Ke,{code:t.text,language:t.lang},n)}case"blockquote":return i("blockquote",{className:"codex-markdown-blockquote",children:m(e.tokens,n)},n);case"list":return Ze(e,n);case"list_item":return Ve(e,n);case"table":return Je(e,n);case"html":{let t=e;return/^<br\s*\/?>$/i.test(t.raw.trim())?i("br",{},n):t.raw}case"math":{let t=e;return i(Be,{value:t.text,display:t.display},n)}default:throw Error(`Unsupported Codex Markdown token: ${JSON.stringify(e)}`)}}function x(e){switch(e.type){case"text":{let n=e;return n.tokens===void 0?v(n.text):n.tokens.map(x).join("")}case"escape":return e.text;case"paragraph":case"heading":case"strong":case"em":case"del":case"link":case"blockquote":case"list_item":return e.tokens.map(x).join("");case"list":return e.items.map(x).join("");case"table":{let n=e;return[n.header,...n.rows].map((t)=>t.map((r)=>r.tokens.map(x).join("")).join("")).join("")}default:return""}}function Ye({token:e,tokenKey:n,offset:t,runs:r,generation:o}){let a=N;try{return N={cursor:t,runs:r,generation:o},te(e,n)}finally{N=a}}var Qe=H(function({result:n}){let{text:t,isStreaming:r}=n,o=B(()=>{let l=n.tokens;if(n.source.includes("!["))Q(l);I(l);let L=new Map,S=0;return l.map((T)=>{let y=ne(T),_=L.get(y)??0;L.set(y,_+1);let A=x(T),re={token:T,tokenKey:`root-${y}-${_}`,offset:S,visibleText:A};return S+=A.length,re})},[n]),a=o.map((l)=>l.visibleText).join(""),s=w({text:"",visibleText:"",runs:[],generation:0}),c=s.current,d=t.startsWith(c.text)||a.startsWith(c.visibleText),u=d?c.visibleText.length:0,k=performance.now(),b=r&&d?c.runs.filter((l)=>k-l.firstSeenAtMs<De):[],C=c.generation+(d?0:1),f={text:t,visibleText:a,runs:b.filter((l)=>l.start<a.length).map((l)=>({...l,end:Math.min(l.end,a.length)})),generation:C};if(r&&a.length>u)f.runs.push({start:u,end:a.length,firstSeenAtMs:k});return X(()=>{s.current=f}),i("div",{dir:"auto",className:"codex-markdown-content codex-markdown-root","data-streaming":r?"true":void 0,children:o.map((l)=>i(Ye,{token:l.token,tokenKey:l.tokenKey,offset:l.offset,runs:f.runs,generation:C},l.tokenKey))})}),en=H(function({text:n,isStreaming:t}){let r=w(null),[o,a]=p(null),[s,c]=p(null);if(g(()=>{let d=ke((u)=>{fe(()=>a(u))},c);return r.current=d,()=>{d.dispose(),r.current=null}},[]),g(()=>{let d=r.current;if(d===null)throw Error("Markdown parser is not mounted");let u=t?Ae(n).markdown:n;d.update({source:u,text:n,isStreaming:t})},[t,n]),s!==null)throw s;return o===null?null:i(Qe,{result:o})});function mn({text:e,streaming:n,host:t}){let r=n===!0,[o,a]=p(e);return g(()=>{if(o===e)return;if(!r){a(e);return}let s=requestAnimationFrame(()=>a(e));return()=>cancelAnimationFrame(s)},[o,r,e]),i(G.Provider,{value:t,children:i(en,{text:r?o:e,isStreaming:r})})}export{mn as CodexMarkdownRenderer};
6
+ `,1)[0];if(t===void 0)throw Error("Codex Markdown token has no first line");let r=t.match(/\S+/)?.[0];return(r===void 0?t:r).slice(0,xe)}function Ge(e){switch(e.type){case"code":return e.raw.trimStart().match(/^(```|~~~)/)?.[1]??"";case"link":return e.href;case"image":{let n=e;return`${n.href}:${q(n.text)}`}case"html":case"space":case"br":case"hr":case"def":return e.raw;case"math":return q(e.text);default:return""}}function ne(e){return`${e.type==="math"?`math-${e.display?"block":"inline"}`:e.type}-${ee(Ge(e))}`}function m(e,n){let t=new Map;return e.map((r)=>{let o=ne(r),a=t.get(o)??0;return t.set(o,a+1),te(r,`${n}-${o}-${a}`)})}function Ve(e,n){let t=e.task?e.tokens.filter((r)=>r.type!=="checkbox"):e.tokens;return h("li",{className:`codex-markdown-list-item${e.task?"codex-markdown-task-list-item":""}`,children:[e.task?h("div",{className:"codex-markdown-task-checkbox",children:[i("input",{type:"checkbox",className:"codex-markdown-checkbox",checked:e.checked,"data-state":e.checked?"checked":"unchecked",readOnly:!0,disabled:!0}),e.checked?i(K,{}):null]}):null,m(t,n)]},n)}function Ze(e,n){let r=`codex-markdown-list ${e.items.some((a)=>a.task)?"codex-markdown-task-list":e.ordered?"codex-markdown-ordered-list":"codex-markdown-unordered-list"}`,o=m(e.items,n);return e.ordered?i("ol",{className:r,start:e.start===""?void 0:e.start,children:o},n):i("ul",{className:r,children:o},n)}function W(e,n,t){let r=t?"th":"td",o=/^\d+$/.test(e.text);return U(r,{key:n,className:`${t?"codex-markdown-table-header":"codex-markdown-table-cell"}${o?" codex-markdown-numeric-table-cell":""}`,align:e.align??void 0},...m(e.tokens,n))}function Je(e,n){let t=new Map;return i("div",{className:"codex-markdown-table-container","data-markdown-table":!0,tabIndex:-1,children:i("div",{className:"codex-markdown-table-scroller",children:i("div",{className:"codex-markdown-table-wrapper",children:h("table",{className:"codex-markdown-table",children:[i("thead",{children:i("tr",{className:"codex-markdown-table-row",children:e.header.map((r,o)=>W(r,`${n}-header-${o}`,!0))})}),i("tbody",{className:"codex-markdown-table-body",children:e.rows.map((r)=>{let o=ee(r.map((c)=>c.text).join("\x00")),a=t.get(o)??0;t.set(o,a+1);let s=`${n}-row-${o}-${a}`;return i("tr",{className:"codex-markdown-table-row",children:r.map((c,d)=>W(c,`${s}-cell-${d}`,!1))},s)})})]})})})},n)}function z(e,n,t){let r=M(e.href);if(!D(r))return null;return i(Xe,{src:r,alt:v(e.text),title:e.title==null?void 0:M(e.title),allowWide:t},n)}function te(e,n){switch(e.type){case"space":case"def":case"internalMessage":return null;case"hr":return i("hr",{className:"codex-markdown-horizontal-rule"},n);case"heading":{let t=e,o={1:"h1",2:"h2",3:"h3",4:"h4",5:"h5",6:"h6"}[t.depth];if(o===void 0)throw Error(`Unsupported Codex Markdown heading depth: ${t.depth}`);return U(o,{key:n,className:`codex-markdown-heading codex-markdown-heading-${t.depth}`},...m(t.tokens,n))}case"paragraph":{let t=e,r=t.tokens.filter((s)=>s.type!=="space"&&s.type!=="br"),o=r.length>0&&r.every((s)=>s.type==="image"),a=o&&r.length>1;return i("p",{className:`codex-markdown-paragraph${o?"codex-markdown-media-paragraph codex-markdown-media-wide-block":""}${a?"codex-markdown-media-grid-paragraph":""}`,"data-markdown-image-grid":a?"true":void 0,"data-wide-markdown-block":o?"true":void 0,"data-wide-markdown-block-kind":o?"image":void 0,children:o?r.map((s,c)=>z(s,`${n}-image-${c}`,!0)):m(t.tokens,n)},n)}case"text":{let t=e;return t.tokens===void 0?P(v(t.text),n):m(t.tokens,n)}case"escape":return P(e.text,n);case"strong":return i("strong",{className:"codex-markdown-strong",children:m(e.tokens,n)},n);case"em":return i("em",{children:m(e.tokens,n)},n);case"del":return i("del",{children:m(e.tokens,n)},n);case"codespan":return i("span",{className:"codex-markdown-inline-code",dir:"ltr","data-markdown-copy":"inline-code",children:e.text},n);case"br":return i("br",{},n);case"link":{let t=e,r=m(t.tokens,n),o=M(t.href);return D(o)?i(We,{href:o,title:t.title==null?void 0:M(t.title),children:r},n):i("span",{children:r},n)}case"image":return z(e,n,!1);case"code":{let t=e;return i(Ke,{code:t.text,language:t.lang},n)}case"blockquote":return i("blockquote",{className:"codex-markdown-blockquote",children:m(e.tokens,n)},n);case"list":return Ze(e,n);case"list_item":return Ve(e,n);case"table":return Je(e,n);case"html":{let t=e;return/^<br\s*\/?>$/i.test(t.raw.trim())?i("br",{},n):t.raw}case"math":{let t=e;return i(Be,{value:t.text,display:t.display},n)}default:throw Error(`Unsupported Codex Markdown token: ${JSON.stringify(e)}`)}}function x(e){switch(e.type){case"text":{let n=e;return n.tokens===void 0?v(n.text):n.tokens.map(x).join("")}case"escape":return e.text;case"paragraph":case"heading":case"strong":case"em":case"del":case"link":case"blockquote":case"list_item":return e.tokens.map(x).join("");case"list":return e.items.map(x).join("");case"table":{let n=e;return[n.header,...n.rows].map((t)=>t.map((r)=>r.tokens.map(x).join("")).join("")).join("")}default:return""}}function Ye({token:e,tokenKey:n,offset:t,runs:r,generation:o}){let a=N;try{return N={cursor:t,runs:r,generation:o},te(e,n)}finally{N=a}}var Qe=H(function({result:n}){let{text:t,isStreaming:r}=n,o=B(()=>{let l=n.tokens;if(n.source.includes("!["))Q(l);I(l);let L=new Map,S=0;return l.map((T)=>{let y=ne(T),_=L.get(y)??0;L.set(y,_+1);let A=x(T),re={token:T,tokenKey:`root-${y}-${_}`,offset:S,visibleText:A};return S+=A.length,re})},[n]),a=o.map((l)=>l.visibleText).join(""),s=w({text:"",visibleText:"",runs:[],generation:0}),c=s.current,d=t.startsWith(c.text)||a.startsWith(c.visibleText),u=d?c.visibleText.length:0,k=performance.now(),b=r&&d?c.runs.filter((l)=>k-l.firstSeenAtMs<De):[],C=c.generation+(d?0:1),f={text:t,visibleText:a,runs:b.filter((l)=>l.start<a.length).map((l)=>({...l,end:Math.min(l.end,a.length)})),generation:C};if(r&&a.length>u)f.runs.push({start:u,end:a.length,firstSeenAtMs:k});return X(()=>{s.current=f}),i("div",{dir:"auto",className:"codex-markdown-content codex-markdown-root","data-streaming":r?"true":void 0,children:o.map((l)=>i(Ye,{token:l.token,tokenKey:l.tokenKey,offset:l.offset,runs:f.runs,generation:C},l.tokenKey))})}),en=H(function({text:n,isStreaming:t}){let r=w(null),[o,a]=p(null),[s,c]=p(null);if(g(()=>{let d=ke((u)=>{fe(()=>a(u))},c);return r.current=d,()=>{d.dispose(),r.current=null}},[]),g(()=>{let d=r.current;if(d===null)throw Error("Markdown parser is not mounted");let u=t?Ae(n).markdown:n;d.update({source:u,text:n,isStreaming:t})},[t,n]),s!==null)throw s;return o===null?i("div",{"data-markdown-pending":!0}):i(Qe,{result:o})});function mn({text:e,streaming:n,host:t}){let r=n===!0,[o,a]=p(e);return g(()=>{if(o===e)return;if(!r){a(e);return}let s=requestAnimationFrame(()=>a(e));return()=>cancelAnimationFrame(s)},[o,r,e]),i(G.Provider,{value:t,children:i(en,{text:r?o:e,isStreaming:r})})}export{mn as CodexMarkdownRenderer};
@@ -1 +1 @@
1
- import{jsx as I,jsxs as ct}from"react/jsx-runtime";import{forwardRef as ut,useCallback as L,useEffect as tt,useImperativeHandle as dt,useLayoutEffect as y,useMemo as k,useRef as u,useState as F}from"react";import{calculateCodexThreadVirtualRange as T,calculateCodexTurnCenterDistanceFromBottom as lt,calculateCodexTurnTargetDistanceFromBottom as xt,createCodexThreadVirtualLayout as gt,preserveCodexThreadDistanceFromBottom as ht,preserveCodexThreadRenderedWindow as et}from"./thread-virtualizer.js";var nt=12,N=2,b=800,rt=new Map,pt=ut(function({entries:v,renderTurn:V,scrollElement:r,sessionKey:C},ot){let x=k(()=>rt.get(C),[C]),[P,W]=F(()=>x?.turnHeightsByKey??{}),n=k(()=>gt({entries:v,gapPx:nt,measuredHeightsByKey:P}),[v,P]),it=k(()=>{let t=T({distanceFromBottomPx:x?.distanceFromBottomPx??0,layout:n,overscanCount:N,viewportHeightPx:b}),e=x?.renderedWindow;if(e===void 0||e===null)return t;return et({anchorKey:e.anchorKey,layout:n,previousRange:{startIndex:0,endIndex:Math.min(e.count,t.endIndex-t.startIndex)}})??t},[x,n]),[p,j]=F(it),[d,A]=F(b),[O,G]=F(x?.distanceFromBottomPx??0),[c,U]=F(null),R=u(O),H=u(p),X=u(n.turnKeys),l=u(P),S=u(n),q=u(n),K=u(new Map),g=u(null),B=u(null),E=u(!1),J=u(()=>{}),h=L((t,e)=>{let o=Math.min(t,n.totalHeightPx),s=T({distanceFromBottomPx:o,layout:n,overscanCount:N,viewportHeightPx:e}),a=H.current,i=a.startIndex<=s.startIndex&&a.endIndex>=s.endIndex?a:s;R.current=o,H.current=i,X.current=n.turnKeys,G(o),A(e),j(i)},[n]),z=L((t)=>{let e=Math.max(0,t.scrollHeight-n.totalHeightPx);return Math.max(0,t.scrollHeight-t.clientHeight-t.scrollTop-e)},[n.totalHeightPx]),_=L(()=>{if(B.current!==null)return B.current;return B.current=new ResizeObserver((t)=>{let e={...l.current},o=!1;for(let s of t){let i=s.target.dataset.turnKey;if(i===void 0)throw Error("Codex measured turn is missing its key");let m=Math.max(1,s.borderBoxSize[0]?.blockSize??s.contentRect.height);if(e[i]===m)continue;e[i]=m,o=!0}if(!o)return;l.current=e,W(e)}),B.current},[]),Q=L((t,e)=>{let o=K.current.get(t);if(o!==void 0)_().unobserve(o),K.current.delete(t);if(e===null)return;if(e.dataset.turnKey!==t)throw Error(`Codex turn key does not match its element: ${t}`);K.current.set(t,e),_().observe(e)},[_]);tt(()=>()=>{B.current?.disconnect(),B.current=null,K.current.clear()},[]),y(()=>{J.current=()=>{if(r===null)return;let t=z(r);if(g.current!==null){R.current=t,G(t),A(r.clientHeight||b);return}h(t,r.clientHeight||b)}},[z,r,h]),y(()=>{if(r===null)return;if(!E.current){E.current=!0;let o=x?.distanceFromBottomPx??0;r.scrollTo({behavior:"instant",top:Math.max(0,o>0?S.current.totalHeightPx-r.clientHeight-o:r.scrollHeight-r.clientHeight)})}let t=()=>J.current();t(),r.addEventListener("scroll",t,{passive:!0});let e=new ResizeObserver(t);return e.observe(r),()=>{r.removeEventListener("scroll",t),e.disconnect()}},[x,r]),y(()=>{if(r===null||c===null)return;let t=K.current.get(c.turnKey);if(t===void 0)return;let e=t.offsetHeight;if(e<=0)return;if(l.current[c.turnKey]!==e){let m={...l.current,[c.turnKey]:e};l.current=m,W(m);return}let o=c.getTargetElement(t)??t,s=t.getBoundingClientRect(),a=o.getBoundingClientRect(),i=xt({align:c.align,layout:n,targetHeightPx:a.height,targetOffsetPx:a.top-s.top,turnKey:c.turnKey,viewportHeightPx:r.clientHeight});if(i===null)throw Error(`Codex virtual turn is missing: ${c.turnKey}`);r.scrollTo({behavior:"instant",top:Math.max(0,n.totalHeightPx-r.clientHeight-i)}),h(i,r.clientHeight||b),queueMicrotask(()=>{if(g.current!==c)return;c.complete(),g.current=null,U(null)})},[n,c,r,h]),y(()=>{let t=q.current;if(q.current=n,S.current=n,r===null||c!==null)return;let e=R.current;if(e>24&&t!==n){let o=z(r);if(o<=96){h(o,d);return}let s=T({distanceFromBottomPx:e,layout:t,overscanCount:0,viewportHeightPx:d}),a=t.turnKeys.slice(s.startIndex,s.endIndex).find((i)=>l.current[i]!==void 0&&n.turnIndexByKey.has(i));if(a!==void 0){let i=ht({anchorKey:a,distanceFromBottomPx:e,nextLayout:n,previousLayout:t});if(i===null)throw Error(`Codex viewport anchor is missing: ${a}`);e=i,r.scrollTo({behavior:"instant",top:Math.max(0,n.totalHeightPx-r.clientHeight-e)})}}h(e,d)},[n,c,z,r,h,d]),y(()=>{l.current=P},[P]),y(()=>{H.current=p},[p]),y(()=>{R.current=O},[O]),tt(()=>()=>{g.current?.complete(),g.current=null;let t=H.current,o=S.current.turnKeys[t.startIndex];rt.set(C,{distanceFromBottomPx:R.current,renderedWindow:o===void 0?null:{anchorKey:o,count:t.endIndex-t.startIndex},turnHeightsByKey:l.current})},[C]),dt(ot,()=>({scrollToKey(t,e=(s)=>s,o={align:"center"}){if(g.current?.complete(),n.turnIndexByKey.get(t)===void 0)throw Error(`Codex virtual turn is missing: ${t}`);let a=lt({layout:n,turnKey:t,viewportHeightPx:d});if(a===null)throw Error(`Codex virtual turn is missing: ${t}`);let i=T({distanceFromBottomPx:a,layout:n,overscanCount:N,viewportHeightPx:d});return H.current=i,j(i),new Promise((m)=>{let Z={align:o.align,complete:m,getTargetElement:e,turnKey:t};g.current=Z,U(Z)})}}),[n,d]);let f=p,M=X.current;if(!(M===n.turnKeys||M.length===n.turnKeys.length&&M.every((t,e)=>t===n.turnKeys[e]))){let t=M[p.startIndex];f=(t===void 0?null:et({anchorKey:t,layout:n,previousRange:p}))??T({distanceFromBottomPx:R.current,layout:n,overscanCount:N,viewportHeightPx:d})}let D=v.length-1,w=v[D],st=n.topOffsetsPx[D]??0,Y=v.slice(f.startIndex,Math.min(f.endIndex,D)),at=n.topOffsetsPx[f.startIndex]??0;return ct("div",{"data-codex-thread":!0,"data-codex-virtualized-pending-key":c?.turnKey,"data-codex-virtualized-range":`${f.startIndex}:${f.endIndex}`,"data-codex-virtualized-total-count":v.length,"data-codex-virtualized-turn-count":Y.length+(w===void 0?0:1),className:"relative shrink-0",style:{height:n.totalHeightPx},children:[I("div",{className:"flex flex-col",style:{gap:nt,marginTop:at},children:Y.map((t,e)=>{let o=f.startIndex+e,s=c?.turnKey!==t.turnKey&&P[t.turnKey]===void 0?n.heightsPx[o]:void 0;return I("div",{style:s===void 0?void 0:{height:s,overflow:"hidden"},children:I("div",{ref:(a)=>Q(t.turnKey,a),className:"[&_[data-virtualized-turn-content]]:[content-visibility:visible]","data-turn-key":t.turnKey,children:V(t,o)})},t.turnKey)})}),w!==void 0&&I("div",{className:"absolute inset-x-0",style:{top:st},children:I("div",{ref:(t)=>Q(w.turnKey,t),className:"[&_[data-virtualized-turn-content]]:[content-visibility:visible]","data-turn-key":w.turnKey,children:V(w,D)})})]})});export{pt as VirtualizedTurnList};
1
+ import{jsx as K,jsxs as lt}from"react/jsx-runtime";import{forwardRef as gt,useCallback as B,useEffect as ot,useImperativeHandle as xt,useLayoutEffect as P,useMemo as A,useRef as c,useState as H}from"react";import{calculateCodexThreadVirtualRange as S,calculateCodexTurnCenterDistanceFromBottom as ht,calculateCodexTurnTargetDistanceFromBottom as ft,createCodexThreadVirtualLayout as mt,preserveCodexThreadDistanceFromBottom as yt,preserveCodexThreadRenderedWindow as it}from"./thread-virtualizer.js";var st=12,V=2,L=800,at=new Map;function ut({turnKey:k,heightPx:x,observe:I,onReady:r,children:p}){let D=c(null),[g,T]=H(!1),b=B((e)=>{D.current=e,I(k,e)},[I,k]);return P(()=>{let e=D.current;if(e===null)throw Error("Codex turn element is missing");let N=()=>{r(e),T(!0)};if(e.querySelector("[data-markdown-pending]")===null){N();return}let d=new MutationObserver(()=>{if(e.querySelector("[data-markdown-pending]")!==null)return;d.disconnect(),N()});return d.observe(e,{childList:!0,subtree:!0}),()=>d.disconnect()},[r]),K("div",{style:g?void 0:{height:x,overflow:"hidden"},children:K("div",{ref:b,className:"[&_[data-virtualized-turn-content]]:[content-visibility:visible]","data-turn-key":k,children:p})})}var wt=gt(function({entries:x,renderTurn:I,scrollElement:r,sessionKey:p},D){let g=A(()=>at.get(p),[p]),[T,b]=H(()=>g?.turnHeightsByKey??{}),e=A(()=>mt({entries:x,gapPx:st,measuredHeightsByKey:T}),[x,T]),N=A(()=>{let t=S({distanceFromBottomPx:g?.distanceFromBottomPx??0,layout:e,overscanCount:V,viewportHeightPx:L}),n=g?.renderedWindow;if(n===void 0||n===null)return t;return it({anchorKey:n.anchorKey,layout:e,previousRange:{startIndex:0,endIndex:Math.min(n.count,t.endIndex-t.startIndex)}})??t},[g,e]),[d,E]=H(N),[h,G]=H(L),[W,U]=H(g?.distanceFromBottomPx??0),[u,X]=H(null),R=c(W),F=c(d),J=c(e.turnKeys),l=c(T),q=c(e),Q=c(e),C=c(new Map),f=c(null),w=c(null),Y=c(!1),Z=c(()=>{}),m=B((t,n)=>{let o=Math.min(t,e.totalHeightPx),s=S({distanceFromBottomPx:o,layout:e,overscanCount:V,viewportHeightPx:n}),a=F.current,i=a.startIndex<=s.startIndex&&a.endIndex>=s.endIndex?a:s;R.current=o,F.current=i,J.current=e.turnKeys,U(o),G(n),E(i)},[e]),O=B((t)=>{let n=Math.max(0,t.scrollHeight-e.totalHeightPx);return Math.max(0,t.scrollHeight-t.clientHeight-t.scrollTop-n)},[e.totalHeightPx]),tt=B((t)=>{let n=t.dataset.turnKey;if(n===void 0)throw Error("Codex measured turn is missing its key");let o=Math.max(1,t.getBoundingClientRect().height);if(l.current[n]===o)return;let s={...l.current,[n]:o};l.current=s,b(s)},[]),j=B(()=>{if(w.current!==null)return w.current;return w.current=new ResizeObserver((t)=>{let n={...l.current},o=!1;for(let s of t){let a=s.target;if(a.querySelector("[data-markdown-pending]")!==null)continue;let i=a.dataset.turnKey;if(i===void 0)throw Error("Codex measured turn is missing its key");let v=Math.max(1,s.borderBoxSize[0]?.blockSize??s.contentRect.height);if(n[i]===v)continue;n[i]=v,o=!0}if(!o)return;l.current=n,b(n)}),w.current},[]),et=B((t,n)=>{let o=C.current.get(t);if(o!==void 0)j().unobserve(o),C.current.delete(t);if(n===null)return;if(n.dataset.turnKey!==t)throw Error(`Codex turn key does not match its element: ${t}`);C.current.set(t,n),j().observe(n)},[j]);ot(()=>()=>{w.current?.disconnect(),w.current=null,C.current.clear()},[]),P(()=>{Z.current=()=>{if(r===null)return;let t=O(r);if(f.current!==null){R.current=t,U(t),G(r.clientHeight||L);return}m(t,r.clientHeight||L)}},[O,r,m]),P(()=>{if(r===null)return;if(!Y.current){Y.current=!0;let o=g?.distanceFromBottomPx??0;r.scrollTo({behavior:"instant",top:Math.max(0,o>0?q.current.totalHeightPx-r.clientHeight-o:r.scrollHeight-r.clientHeight)})}let t=()=>Z.current();t(),r.addEventListener("scroll",t,{passive:!0});let n=new ResizeObserver(t);return n.observe(r),()=>{r.removeEventListener("scroll",t),n.disconnect()}},[g,r]),P(()=>{if(r===null||u===null)return;let t=C.current.get(u.turnKey);if(t===void 0||t.querySelector("[data-markdown-pending]")!==null)return;let n=t.offsetHeight;if(n<=0)return;if(l.current[u.turnKey]!==n){let v={...l.current,[u.turnKey]:n};l.current=v,b(v);return}let o=u.getTargetElement(t)??t,s=t.getBoundingClientRect(),a=o.getBoundingClientRect(),i=ft({align:u.align,layout:e,targetHeightPx:a.height,targetOffsetPx:a.top-s.top,turnKey:u.turnKey,viewportHeightPx:r.clientHeight});if(i===null)throw Error(`Codex virtual turn is missing: ${u.turnKey}`);r.scrollTo({behavior:"instant",top:Math.max(0,e.totalHeightPx-r.clientHeight-i)}),m(i,r.clientHeight||L),queueMicrotask(()=>{if(f.current!==u)return;u.complete(),f.current=null,X(null)})},[e,u,r,m]),P(()=>{let t=Q.current;if(Q.current=e,q.current=e,r===null||u!==null)return;let n=R.current;if(n>24&&t!==e){let o=O(r);if(o<=96){m(o,h);return}let s=S({distanceFromBottomPx:n,layout:t,overscanCount:0,viewportHeightPx:h}),a=t.turnKeys.slice(s.startIndex,s.endIndex).find((i)=>l.current[i]!==void 0&&e.turnIndexByKey.has(i));if(a!==void 0){let i=yt({anchorKey:a,distanceFromBottomPx:n,nextLayout:e,previousLayout:t});if(i===null)throw Error(`Codex viewport anchor is missing: ${a}`);n=i,r.scrollTo({behavior:"instant",top:Math.max(0,e.totalHeightPx-r.clientHeight-n)})}}m(n,h)},[e,u,O,r,m,h]),P(()=>{F.current=d},[d]),P(()=>{R.current=W},[W]),ot(()=>()=>{f.current?.complete(),f.current=null;let t=F.current,o=q.current.turnKeys[t.startIndex];at.set(p,{distanceFromBottomPx:R.current,renderedWindow:o===void 0?null:{anchorKey:o,count:t.endIndex-t.startIndex},turnHeightsByKey:l.current})},[p]),xt(D,()=>({scrollToKey(t,n=(s)=>s,o={align:"center"}){if(f.current?.complete(),e.turnIndexByKey.get(t)===void 0)throw Error(`Codex virtual turn is missing: ${t}`);let a=ht({layout:e,turnKey:t,viewportHeightPx:h});if(a===null)throw Error(`Codex virtual turn is missing: ${t}`);let i=S({distanceFromBottomPx:a,layout:e,overscanCount:V,viewportHeightPx:h});return F.current=i,E(i),new Promise((v)=>{let rt={align:o.align,complete:v,getTargetElement:n,turnKey:t};f.current=rt,X(rt)})}}),[e,h]);let y=d,_=J.current;if(!(_===e.turnKeys||_.length===e.turnKeys.length&&_.every((t,n)=>t===e.turnKeys[n]))){let t=_[d.startIndex];y=(t===void 0?null:it({anchorKey:t,layout:e,previousRange:d}))??S({distanceFromBottomPx:R.current,layout:e,overscanCount:V,viewportHeightPx:h})}let M=x.length-1,z=x[M],ct=e.topOffsetsPx[M]??0,nt=x.slice(y.startIndex,Math.min(y.endIndex,M)),dt=e.topOffsetsPx[y.startIndex]??0;return lt("div",{"data-codex-thread":!0,"data-codex-virtualized-pending-key":u?.turnKey,"data-codex-virtualized-range":`${y.startIndex}:${y.endIndex}`,"data-codex-virtualized-total-count":x.length,"data-codex-virtualized-turn-count":nt.length+(z===void 0?0:1),className:"relative shrink-0",style:{height:e.totalHeightPx},children:[K("div",{className:"flex flex-col",style:{gap:st,marginTop:dt},children:nt.map((t,n)=>{let o=y.startIndex+n;return K(ut,{turnKey:t.turnKey,heightPx:e.heightsPx[o],observe:et,onReady:tt,children:I(t,o)},t.turnKey)})}),z!==void 0&&K("div",{className:"absolute inset-x-0",style:{top:ct},children:K(ut,{turnKey:z.turnKey,heightPx:e.heightsPx[M],observe:et,onReady:tt,children:I(z,M)},z.turnKey)})]})});export{wt as VirtualizedTurnList};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alwith/module-chat",
3
- "version": "0.1.6",
3
+ "version": "0.1.9",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {